﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections.Specialized;

public partial class report : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btn_ok_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            btn_ok.Enabled = false;

            NameValueCollection args = Request.Form;

            if (args != null)
            {
                string strUserName = args["userName"].Trim();
                string strUserPhone = args["userPhone"].Trim();
                string strCardType = args["cardType"].Trim();
                string strCardNumber = args["cardNumber"].Trim();

                string strpollname = args["pollname"].Trim();
                string strpolltype = args["polltype"].Trim();
                string strlat = args["lat"].Trim();
                string strlon = args["lon"].Trim();
                string strcaseDesc = args["caseDesc"].Trim();
                Random ran = new Random();
                string strSerial = ran.Next(999999).ToString();
                string strPass = ran.Next(999999).ToString();

                string strFilePath = string.Empty;

                if (!FileUpLoad(ref strFilePath))
                {
                    strFilePath = string.Empty;
                }
                

                string strsql = String.Format(@"INSERT INTO [pollution](
                                                                [p_name],
                                                                [p_ctdt], 
                                                                [p_type],
                                                                [p_descation],
                                                                [p_img],
                                                                [p_state],
                                                                [u_id],
                                                                [p_rname],
                                                                [p_ridtype],
                                                                [p_r_idcard],
                                                                [p_rtel],                                                               
                                                                [p_serial],
                                                                [p_passwd], 
                                                                [p_lat],
                                                                [p_lon]
                                                                ) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}')",
                                                               strpollname,
                                                               DateTime.Now,
                                                               strpolltype,
                                                               strcaseDesc,
                                                               strFilePath,
                                                               0,
                                                               0,
                                                               strUserName,
                                                               strCardType,
                                                               strCardNumber,
                                                               strUserPhone,
                                                               strSerial,
                                                               strPass,
                                                               strlat,
                                                               strlon
                                                               );
                ManageData mg = new ManageData(util.DATACONNECTION);
                if (mg != null)
                {
                    if (mg.ExecNonQuery(strsql) > 0 )
                    {

                        Response.Redirect(string.Format("./reportsucc.aspx?s={0}&p={1}", strSerial, strPass));


                    }
                }                                                                                       
            }          
        }
    }

    private bool FileUpLoad(ref string filepath)
    {
        bool bRet = true;
        if (FileUpLoad1.HasFile)
        {
            //判断文件是否小于10Mb  
            if (FileUpLoad1.PostedFile.ContentLength < 10485760)
            {
                try
                {
                    filepath = util.GetRandomName(FileUpLoad1.FileName);
                    //上传文件并指定上传目录的路径  
                    FileUpLoad1.PostedFile.SaveAs(Server.MapPath("~/upload/") + filepath);

                }
                catch (Exception)
                {
                    bRet = false;
                }

            }
            else
            {
                bRet = false;
            }
        }
        else
        {
            bRet = false;
        }

        return bRet;
    }
}
